package com.zjxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.Admin;
import com.zjxf.base.bean.po.SchoolTutor;
import com.zjxf.base.bean.ro.RoSchoolTutor;
import com.zjxf.base.bean.vo.VoSchoolTutor;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.controller.common.BaseController;
import com.zjxf.service.SchoolTutorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 学校导师
 *
 * @author ZhangWei
 * @date 2021/3/29/029 10:34
 */
@Validated
@RestController
@AllArgsConstructor
@Api(tags = "学校导师")
@RequestMapping("/schoolTutor/")
public class SchoolTutorController extends BaseController {

    private final SchoolTutorService schoolTutorService;

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    public JSONResponse delete(@NotNull(message = "请填写学校导师id") @RequestParam Long id) throws OperationException {
        schoolTutorService.delete(id);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "回显")
    @PostMapping("getEntity")
    public JSONResponse getEntity(@NotNull(message = "请填写学校新闻id") @RequestParam Long id) throws OperationException {
        SchoolTutor schoolTutor = schoolTutorService.getEntity(id);
        RoSchoolTutor.entity roSchoolTutor = schoolTutorService.getEntityRo(schoolTutor);
        return JSONResponse.success(roSchoolTutor);
    }

    @ApiOperation(value = "分页")
    @PostMapping("page")
    public JSONResponse page(@Valid @RequestBody VoSchoolTutor.filter filter) {
        Page<SchoolTutor> schoolTutorPage = schoolTutorService.page(filter);
        JSONObject jsonData = schoolTutorService.conversion(schoolTutorPage);
        return JSONResponse.success(jsonData);
    }

    @ApiOperation(value = "审核")
    @PostMapping("/check")
    public JSONResponse check(@Valid @RequestBody VoSchoolTutor.check check) throws OperationException {
        Admin admin = (Admin) getUser();
        schoolTutorService.check(check, admin);
        return JSONResponse.success(SysConst.SUCCESS);
    }
}
